<?php

// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once ($AppUI->getSystemClass('dp')); // use the dPFramework for easy inclusion of this class here

/**
 * The CMdType Class
 */
class CMdType extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $type_id = NULL; //use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $code = NULL; //use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $name = NULL; //use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $type = NULL; //use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $active = 1;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdType() {
		$joins = array(
			array( 'label' => 'Tasks', 'name' => 'tasks', 'idfield' => 'task_id', 'joinfield' => 'task_type' ),
			array( 'label' => 'Unit', 'name' => 'md_unit', 'idfield' => 'unit_id', 'joinfield' => 'unit_type' ),
			array( 'label' => 'Status', 'name' => 'md_status', 'idfield' => 'status_id', 'joinfield' => 'status_type' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_type1' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_type2' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_type3' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_type4' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_type5' ),
			array( 'label' => 'Reference', 'name' => 'projects', 'idfield' => 'project_id', 'joinfield' => 'project_rtype' ),
			array( 'label' => 'Reference', 'name' => 'projects', 'idfield' => 'project_id', 'joinfield' => 'project_shipping_type' ),
			array( 'label' => 'Quotation', 'name' => 'quotations', 'idfield' => 'quote_id', 'joinfield' => 'quote_type' ),
			array( 'label' => 'Quotation', 'name' => 'quotations', 'idfield' => 'quote_id', 'joinfield' => 'quote_shipping_type' ),
			array( 'label' => 'Transportation', 'name' => 'transportations', 'idfield' => 'trans_id', 'joinfield' => 'trans_type' ),
			array( 'label' => 'Resource', 'name' => 'users', 'idfield' => 'user_id', 'joinfield' => 'user_type' ),
			array( 'label' => 'classification', 'name' => 'classifications', 'idfield' => 'cls_id', 'joinfield' => 'cls_type' ),
			array( 'label' => 'Customer', 'name' => 'companies', 'idfield' => 'company_id', 'joinfield' => 'company_type2' )
		);

		$this->CDpObject('md_type', 'type_id', 'code', $joins, 'masterdata' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->type_id");
		$q->addWhere("$this->_tbl_unique = '$this->code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate type code: ' . $this->code;
		}
		return NULL;
	}

	/**
	* Get type list by of specify type, etc Reference, Quotation, Item...
	* allow only active = 1 type
	*/
	function getTypeList($t = null, $required = FALSE, $fetchObject = FALSE) {
		return getTypeList($t, $required, $fetchObject);
	}
}

function getTypeList($t = null, $required = FALSE, $fetchObject = FALSE) {
		$q = new DBQuery;
		$q->addTable('md_type');
		if ($fetchObject == FALSE) {
			$q->addQuery('type_id, name');
		}
		if ($t != null || $t > 0) {
			$q->addWhere('type =\'' . $t . '\'');
		}
		$q->addWhere('active = 1');
		$q->addOrder('name');
		if ($fetchObject) {
			$arr = $q->loadList();
		} else {
			$arr = $q->loadHashList();
		}
		if($required){
			return $arr;
		}
		if ($fetchObject) {
			$blank = array('type_id' => '', 'code' => '', 'name' => '');
			return arrayMerge(array(-1 => $blank), $arr);
		}
		return arrayMerge(array('' => ''), $arr);
	}
//Container|Container
//Customer|Customer
//ItemGroup|Item group
//ItemType1|ItemType1
//ItemType3|ItemType3
//ItemType4|ItemType4
//ItemType5|ItemType5
//Quotation|Quotation
//Reference|Reference
//Resource|Resource
//ShippingType|ShippingType
//Supplier|Supplier
//Task|Task
//Transportation|Transportation
//Unit|Unit
?>
